package com.toogu.mazhangmen.ui.monitor;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.ui.view.RadioGroupView;
import com.toogu.mazhangmen.util.PopUtil;
import com.toogu.mazhangmen.util.UiUtil;

import static com.toogu.mazhangmen.util.UiUtil.createButton;
import static com.toogu.mazhangmen.util.UiUtil.createText;

public class ControlView implements ControlContract.IView {
    private Activity activity = null;
    private View rootView;
    private Delegate delegate;
    private EquipmentsBean equipmentsBean;

    private LinearLayout layoutControl;
    private LinearLayout layoutPerson;

    private ImageView imageViewPerson;
    private ImageView imageViewCar;

    private TextView shouldPay;
    private Button takePhoto;
    private Button paySubmit;



    public ControlView(Activity activity, View rootView, EquipmentsBean equipmentsBean){
        this.activity=activity;
        this.rootView=rootView;
        this.equipmentsBean=equipmentsBean;
    }
    @Override
    public void onCreate() {
        LinearLayout layout = rootView.findViewById(R.id.layout);
        layoutControl = createLinearLayout(LinearLayout.VERTICAL);
        layoutPerson = createLinearLayout(LinearLayout.VERTICAL);
        layout.addView(layoutControl);
        layout.addView(layoutPerson);
        setControl();
    }

    public void setControl(){
        if(equipmentsBean.getDoor()==null&&equipmentsBean.getDoors()!=null) {
            TextView title = new TextView(activity);
            title.setText("选择门");
            title.setTextColor(Color.WHITE);
            layoutControl.addView(title);
            RadioGroupView radioGroupView = new RadioGroupView(activity);
            layoutControl.addView(radioGroupView);
            String[] doorName = new String[0];
            doorName = new String[equipmentsBean.getDoors().size()];
            for (int i = 0; i < equipmentsBean.getDoors().size(); i++) {
                String name = equipmentsBean.getDoors().get(i).getName();
                if (name != null) doorName[i] = name;
                else doorName[i] = "无名";
            }
            radioGroupView.setInit(doorName);
            radioGroupView.setOnCheckedChangeListener(delegate.getOnCheckedChangeListener());
            radioGroupView.select(0);
        }
        boolean isClickable = !(equipmentsBean.getDoor() == null && equipmentsBean.getDoors() ==null);

        TextView title=new TextView(activity);
        title.setText("控制按钮");
        title.setTextColor(Color.WHITE);
        layoutControl.addView(title);
        LinearLayout tempLayout = createLinearLayout(LinearLayout.HORIZONTAL);
        layoutControl.addView(tempLayout);
        tempLayout.addView(createButton(activity,1,"开全门(自动)",isClickable,delegate.getControlListener()));
        tempLayout.addView(createButton(activity,2,"开全门(手动)",isClickable,delegate.getControlListener()));
        tempLayout.addView(createButton(activity,3,"关门",isClickable,delegate.getControlListener()));
        tempLayout = createLinearLayout(LinearLayout.HORIZONTAL);
        layoutControl.addView(tempLayout);
        tempLayout.addView(createButton(activity,4,"开半门(自动)",isClickable,delegate.getControlListener()));
        tempLayout.addView(createButton(activity,5,"开半门(手动)",isClickable,delegate.getControlListener()));
        tempLayout.addView(createButton(activity,6,"对讲",isClickable,delegate.getControlListener()));
        tempLayout = createLinearLayout(LinearLayout.HORIZONTAL);
        layoutControl.addView(tempLayout);
        tempLayout.addView(createButton(activity,7,"打开警报",isClickable,delegate.getControlListener()));
        tempLayout.addView(createButton(activity,8,"关闭警报",isClickable,delegate.getControlListener()));
        tempLayout.addView(createButton(activity,9,"打开音频",isClickable,delegate.getControlListener()));
        tempLayout = createLinearLayout(LinearLayout.HORIZONTAL);
        layoutControl.addView(tempLayout);
        tempLayout.addView(createButton(activity,10,"处理完成",isClickable,delegate.getControlListener()));
        tempLayout.addView(createButton(activity,11,"问题反馈",isClickable,delegate.getControlListener()));
        Button button =createButton(activity,-1,"",isClickable,delegate.getControlListener());
        button.setVisibility(View.INVISIBLE);
        tempLayout.addView(button);
    }

    @Override
    public void setPerson(ControlContract.AccessOutRecordBean accessOutRecordBean){
        layoutPerson.removeAllViews();
        if(accessOutRecordBean==null) return;
        TextView title=new TextView(activity);
        title.setText("实时人员信息");
        title.setTextColor(Color.WHITE);
        layoutPerson.addView(title);
        View picture = LayoutInflater.from(activity).inflate(R.layout.item_picture, null, false);
        layoutPerson.addView(picture);
        imageViewCar = picture.findViewById(R.id.imageViewCar);
        imageViewPerson=picture.findViewById(R.id.imageViewPerson);

        Glide.with(activity)
                .load(accessOutRecordBean.getUser().getIdentification_url())
                .fallback(R.drawable.ic_temp)
                .placeholder(R.drawable.ic_temp)
                .into(imageViewPerson);
        Glide.with(activity)
                .load(accessOutRecordBean.getCar_img())
                .fallback(R.drawable.ic_temp)
                .placeholder(R.drawable.ic_temp)
                .into(imageViewCar);

        imageViewPerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PopUtil().showPicture(activity,accessOutRecordBean.getUser().getIdentification_url());
            }
        });
        imageViewCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PopUtil().showPicture(activity,accessOutRecordBean.getCar_img());
            }
        });

        if (accessOutRecordBean.getUser_month_info_id() != null && Integer.valueOf(accessOutRecordBean.getUser_month_info_id()) > 0) {//月卡用户
            layoutPerson.addView(createText(activity,"车辆状态：月卡 " + accessOutRecordBean.getUser_month_info().getEnd_at() + "到期"));
            layoutPerson.addView(createText(activity,"姓名：" + (accessOutRecordBean.getUser().getName()==null?"无数据": accessOutRecordBean.getUser().getName())));
            layoutPerson.addView(createText(activity,"车卡：" + (accessOutRecordBean.getFamily_help().equals("1") ? "共享人员代取" : "月租本人")));
            layoutPerson.addView(createText(activity,"人员状态：" + accessOutRecordBean.getStatus_text()));
            layoutPerson.addView(createText(activity,"联系电话：" + accessOutRecordBean.getUser().getMobile()));
            layoutPerson.addView(createText(activity,accessOutRecordBean.getType().equals("1")?"入场时间：":"出场时间：" + accessOutRecordBean.getCreated_at()));
            layoutPerson.addView(createText(activity,"车卡电量：" + (accessOutRecordBean.getUser_month_info().getMonthCard().getPower()==null?"无数据":accessOutRecordBean.getUser_month_info().getMonthCard().getPower())));
        } else {//临时用户
            layoutPerson.addView(createText(activity,"车辆状态：临停"));
            layoutPerson.addView(createText(activity,"联系电话：" + (accessOutRecordBean.getUser().getMobile()==null?"无数据":accessOutRecordBean.getUser().getMobile())));
            layoutPerson.addView(createText(activity,accessOutRecordBean.getType().equals("1")?"入场时间：":"出场时间：" + accessOutRecordBean.getCreated_at()));

            shouldPay = createText(activity,"停车费用：未抓拍");
            layoutPerson.addView(shouldPay);
            layoutPerson.addView(createText(activity,""));

            LinearLayout linearLayout = createLinearLayout(LinearLayout.HORIZONTAL);
            takePhoto = createButton(activity,1, "抓拍", true, delegate.getTakePhoto());
            takePhoto.setTag(accessOutRecordBean);
            paySubmit = createButton(activity,2, "确认付费", true, delegate.getPaySubmit());
            paySubmit.setVisibility(View.INVISIBLE);
            linearLayout.addView(takePhoto);
            linearLayout.addView(paySubmit);
            layoutPerson.addView(linearLayout);
        }

    }

    @Override
    public void showPay(ControlContract.TempRecord tempRecord) {
        shouldPay.setText("停车费用："+tempRecord.temporaryFee);
        paySubmit.setTag(tempRecord.temporaryRecord.id);
        paySubmit.setVisibility(View.VISIBLE);
    }

    @Override
    public void showPersonImage(String url) {
        Glide.with(activity)
                .load(url)
                .fallback(R.drawable.ic_temp)
                .placeholder(R.drawable.ic_temp)
                .into(imageViewPerson);
    }

    public LinearLayout createLinearLayout(int Side){
        LinearLayout linearLayout = new LinearLayout(activity);
        linearLayout.setOrientation(Side);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.setBaselineAligned(false);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.topMargin = UiUtil.dip2px(5);
        linearLayout.setLayoutParams(layoutParams);
        return linearLayout;
    }



    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestory() {

    }

    @Override
    public void setViewDelegate(Delegate delegate) {
        this.delegate=delegate;
    }
}
